<?php
//模块名_控制名
class Warehouse_PrintDocumentController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "warehouse/repeatprint/";	//控制对应模板目录
	}
	
	public function indexAction(){		
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function getdocumentAction(){
    	$number_code = $this->_request->getParam('number_code','');
    	$type = $this->_request->getParam('type','');
    	$data = Warehouse_Service_ReprintProccess::getDocument(trim($number_code),$type);    	

		die(Zend_Json::encode($data));
    }
    
    public function reprintOrderAction(){	//重新打印配货单
//     	set_time_limit(0);//禁用超时
    	$orderCodes = $this->_request->getParam('number_codes','');
    	$result = $rows = array();
    	$category = '';
    	foreach($orderCodes as $orderCode){
// 			$orderProcess = new Order_Service_OrderProccess($orderCode);			
			$order =Order_Service_Order::getByOrdersCode($orderCode);
			$cond = array('orders_code'=>$orderCode);	 //$orderCode 为订单号
    		// 由订单号从order_pickup_map表获取 配货单与订单号 的对应信息
    		$rows = Product_Service_Pickup::getPickupMap($cond);
    		$shipOrder = Order_Service_OrderShip::getByfield('orders_code',$orderCode);
    		$peihuoUser = User_Service_User::getByUserId($order['pickup_user_id']);
    		$order['peihuo'] = $peihuoUser['name'];
    		$order['page'] = 0;
    		
    		$customer = Customer_Service_Customer::getByCustomerId($order['customer_id']);
    		if($customer['customer_logo']){
    			$order["customer_logo"] =  "/product/product/view-img/customer_id/".$order['customer_id']."/pa_path/".$customer['customer_logo']."";
    		}
    		$order["customer_intro"] = $customer['customer_intro'];
    		
//    		print_r($rows);continue;
    		if($rows){
    			
    			foreach ($rows as $key => $val){
	
    				$val['batch']['take'] = $val['opm_quantity'];
    				$val['batch']['product_sku'] = $val['product']['product_sku'];
    				$val['batch']['product_title'] = empty($val['product']['product_title_cn'])?$val['product']['product_title']:$val['product']['product_title_cn'];
    				$category = Product_Service_Product::getProductCategory($val['product']['product_id']);    			
    				

    				$val['batch']['poc_value_shortname'] = $category['poc_value_shortname'];
					if(empty($order['pp_name'])){
						$row_PackageMap = Product_Service_PackageMap::getAllByProductId ( $val['product_id'] );
							
						if($row_PackageMap){
							$row_package = Product_Service_Package::getByPpid ( $row_PackageMap ['package_id'] );
							$val['batch']['pp_name'] = $row_package['pp_name'];
							$order['pp_name'] = $row_package['pp_name'];
						}
						
					}
    				$val['batch']["pic"] = "/product/product/view-picture/id/" . ($val['product']["product_id"]);
    				// 					产品缩略图

//     				if(isset($order["product_batch"][$val['batch']['ws_code']])){
//     					$order["product_batch"][$val['batch']['ws_code']]['take'] += $val['batch']['take'];    					
//     				}else{
//     					$order["product_batch"][$val['batch']['ws_code']] = $val['batch'];    					
//     				}
    				$order["product_batch"][$val['batch']['ws_code']."_".$val['opm_id']] = $val['batch'];
//     				$order['order_product'][$key2]['bantch'] = ksort($order['order_product'][$key2]['bantch']);
    				$order['ws_code'] = $val['batch']['ws_code'];  
    				
    				

    					
    				
    				$result['code'] = $val['op_code'];
    			}
    			ksort($order["product_batch"]);
    			foreach($order['product_batch'] as $v_){
    				$order['ws_code']=$v_['ws_code'];//取得第一个产品的货架
    				break;
    			}
    			
//    			$order['ship_order'] = $shipOrder[0]['tracking_number'];

    			
    			$chunk = array_chunk($order["product_batch"],7);//每页最多9个产品
    			foreach($chunk as $k0=>$v0){
    				$order['product_batch'] = $v0;
    				$order['page'] = $k0;
    				$result["orders"][$order['pickup_user_id']."__".$order['ws_code']."__".$order['order_id']."__".$k0] = $order;
    			}
    			
//     			$result["orders"][$order['ws_code']."__".$order['order_id']] = $order; 												
			}
			
			
// 			print_r($order);exit;
//          日志
			Warehouse_Service_ReprintProccess::createLog($orderCode,"order product re-print",$order['orders_status']);
		}
		ksort($result["orders"]);
		
		$kkkkk= 0;
		foreach($result["orders"] as $o){//顺序回写
			$kkkkk++;
			$row['order_print_id'] = $kkkkk;
			Order_Service_Order::updateByOrdersCode($row,$o['orders_code']);
		}
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;     //操作人
		$result['admin'] = $session->name;
		$result['orders_count'] = count($orderCodes);
		$result['category'] = $category;
		$result['date'] = date("Y-m-d");
//		$result['code'] = $rows[0]['op_code'];
//		$printResultSession = new Zend_Session_Namespace("printResult");
//		$printResultSession->result = $result;
// 		print_r($result);exit;
		$this->view->result = $result;
		$this->view->err_orders = json_encode(array());
		
    	echo $this->view->render("product/offshelf/reprint-order.tpl");
    }
    
    public function reprintOffshelfAction(){	//重新打印 下架单
    	$result = array();
    	$category = '';
    	$code = $this->_request->number_codes;
    	
    	$cond = array('op_code'=>$code[0]);
    	$rows = Product_Service_Pickup::getPickupMap($cond);

    	foreach($rows as $key => $val){    		
			$orderProcess = new Order_Service_OrderProccess($val['orders_code']);
			$order = $orderProcess->getOrder();
			$result["orders"][] = $order;
			$category = $order["order_product"][0]['category'];
		}
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;     //操作人
		$result['admin'] = $session->name;
		$result['category'] = $category;
		$result['date'] = date("Y-m-d");
		$result['code'] = $code[0];
		
		$this->view->result = $result;
// 		print_r($result);exit;
    	echo $this->view->render("warehouse/repeatprint/print.tpl");
    }
    
    public function reprintReceivingAction(){	//重新打印 入库单
    	$code = $this->_request->number_codes;
    	$receivingCode = trim($code[0]);
		$condition = array("receiving_code" => $receivingCode);
		
		$receiving = Product_Service_Receiving::getByCondition($condition);
		$items = array();
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		$session = new Zend_Session_Namespace('userAuth');
		$this->view->userName=$session->name;
		$this->view->currentDate = date("Y年m月d日");
		$this->view->receiving = $receiving[0];
		$this->view->items = $items;
		echo $this->view->render('product/receiving/detail.tpl');
    }
    
    public function reprintPutawayAction(){	//重新打印 上架单
    	$code = $this->_request->number_codes;
    	$putaway_code = trim($code[0]);    	
		if($putaway_code==''){
			die("操作失败!");
		}
		$session = new Zend_Session_Namespace('userAuth');
		$user_name=$session->name;					//操作人
		$contion=array(
			'putaway_code'=>$putaway_code,
		);
		$result = Product_Service_Putaway::getByCondition($contion,"*");
		foreach($result as $key => $val){
			$rows=Product_Service_Product::getByProductId($val['product_id']);
			$result[$key]['product_sku']=$rows['product_sku'];
		}
		$receiving_code=$result[0]['receiving_code'];
		$msrRow=Product_Service_Receiving::getByReceivingCode($receiving_code);
		$rows=array(
			'customer_id'=>$msrRow['customer_id'],
			'receiving_code'=>$receiving_code,
			'putaway_code'=>$putaway_code,
			'name'=>$user_name,
			'date'=>date('Y-m-d H:i:s'),
			'description'=>$msrRow['description'],
		);
		$this->view->info = $rows;
		$this->view->result = $result;
		echo Cff::renderTpl('product/putaway/print-putaway.tpl',"layoutheader");
    	
    }
    

    public function hotPrintAction(){	//重新 热敏打印
    	
    	$result = $orderlist = array();
    	$category = '';
    	$code = $this->_request->number_codes;	//一维数组，订单号
    	$isIE = $this->_request->getParam('isIE','0');//浏览器判断
    	
    	
    	foreach ($code as $codeKey => $orderCode){
    	
    		$cond = array('orders_code'=>$orderCode);	 //$orderCode 为订单号
    		// 由订单号从order_pickup_map表获取 配货单与订单号 的对应信息
    		$rows = Product_Service_Pickup::getPickupMap($cond);
    		$rows_clone = array();
			$shipOrder = Order_Service_OrderShip::getLastRowByOrdersCode($orderCode);

    		if($rows && $shipOrder){
    			
    		 	$orderProcess = new Order_Service_OrderProccess($orderCode);
				$order = $orderProcess->getOrder();
				$peihuoUser = User_Service_User::getByUserId($order['pickup_user_id']);
				$order['peihuo'] = $peihuoUser['name'];
    			foreach ($rows as $key => $val){
    				$val['take'] = $val['opm_quantity'];
    				$val['ws_code'] = $val['batch']['ws_code'];
    				$val['product_sku'] = $val['product']['product_sku'];
    				$category = Product_Service_Product::getProductCategory($val['product']['product_id']);
    			
    				$val['poc_value_shortname'] = "";
    				if($category){    				
    					$val['poc_value_shortname'] = $category['poc_value_shortname'];
    				}
    				$val['product_title'] = $val['product']['product_title_cn']?$val['product']['product_title_cn']:$val['product']['product_title'];    			    			
    				$rows_clone[$val['batch']['ws_code']."__".$val['batch']['wpb_id']] = $val;
    				$order['ws_code'] = $val['batch']['ws_code']; 
    			}
//  			print_r($rows_clone);exit;
				
// 				
				ksort($rows_clone);
				$order["product_bantch"] = $rows_clone;
			
				$chunk = array_chunk($order['product_bantch'],5);//产品5个一页
				if(count($chunk)>1){//第一个和最后一个互换
					$chunk = array_reverse($chunk,true);				
					$temp = $chunk[0];	
					$chunk[0] = $chunk[count($chunk)-1];
					$chunk[count($chunk)-1] = $temp;	
					
				}
				$count = 0;			
				$order['page'] = 0;			
				foreach($chunk as $k=>$v){
					$order['product_bantch'] = $v;	
					$order['ship_order'] = $shipOrder['tracking_number'];				
					$order['page'] = $k;					
					$orderlist[$order['ws_code']."__".$order['order_id']] =  $order; 
					$count++;				
				}
			}
		}
		ksort($orderlist);
		$result['isIE'] = $isIE;
		$result['orders_count'] = count($code);
		$result["orders"] = $orderlist;
		$op_code = $rows[0]['op_code'];
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;     //操作人
		$result['admin'] = $session->name;
		$result['date'] = date("Y-m-d");
		$result['code'] = $op_code;
// 		print_r($result);exit;
		$this->view->result = $result;
//		echo $this->view->render($this->tplDirectory."hot-print-detail.tpl");
		echo $this->view->render("product/offshelf/hot-print-detail_new1.tpl");
    	
    	    	
    }
    

    
}